﻿// --------------------------------
// <copyright file="AddCalculationForm.cs" company="TRONSoft">
//     Copyright (c) TRONSoft.
// </copyright>
// <license>
//   This source code is subject to terms and conditions of the Microsoft
//   Public License. A copy of the license can be found in the License.html
//   file at the root of this distribution. If you cannot locate the
//   Microsoft Public License, please send an email to dlr@microsoft.com.
//   By using this source code in any fashion, you are agreeing to be bound 
//   by the terms of the Microsoft Public License. You must not remove this
//   notice, or any other, from this software.
// </license>
// <author>Tron</author>
// ---------------------------------

using System;
using System.ComponentModel;
using System.Drawing;
using ICSharpCode.TextEditor;
using ICSharpCode.TextEditor.Document;
using WeifenLuo.WinFormsUI.Docking;

namespace Actucal
{
    public partial class AddCalculationForm : DockContent
    {
        public AddCalculationForm()
        {
            InitializeComponent();

            ProductsService.CurrentProductChanged += ProductsService_CurrentProductChanged;

            txtSourceEditor.TextEditorProperties.ShowSpaces = false;
            txtSourceEditor.TextEditorProperties.ShowTabs = false;
            txtSourceEditor.TextEditorProperties.ShowEOLMarker = false;
            txtSourceEditor.Document.HighlightingStrategy = HighlightingManager.Manager.FindHighlighter("VBNET");

            // Select all text
            TextArea textArea = txtSourceEditor.ActiveTextAreaControl.TextArea;
            SelectAll(textArea);
        }

        public string CalculationName
        {
            get { return txtName.Text; }
            set { txtName.Text = value; }
        }

        public string CalculationCode
        {
            get { return txtSourceEditor.Text; }
            set { txtSourceEditor.Text = value; }
        }

        public static event EventHandler CalculationAdded;

        protected static void OnCalculationAdded(object sender, EventArgs e)
        {
            if (null != CalculationAdded)
                CalculationAdded(sender, e);
        }

        private void ProductsService_CurrentProductChanged(object sender, ArgumentEventArgs<Product> e)
        {
            Enabled = (ProductsService.CurrentProduct != null);
        }

        private void SelectAll(TextArea textArea)
        {
            textArea.AutoClearSelection = false;
            Point startPoint = new Point(0, 0);
            Point endPoint = textArea.Document.OffsetToPosition(textArea.Document.TextLength);
            if (textArea.SelectionManager.HasSomethingSelected)
            {
                if (textArea.SelectionManager.SelectionCollection[0].StartPosition == startPoint &&
                    textArea.SelectionManager.SelectionCollection[0].EndPosition == endPoint)
                {
                    return;
                }
            }
            textArea.SelectionManager.SetSelection(new DefaultSelection(textArea.Document, startPoint, endPoint));
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            ValidateChildren();

            bool attributeExists = ProductsService.CurrentProduct.ExistsAttribute(CalculationName);

            ProductsService.CurrentProduct.Attributes[CalculationName] = CalculationCode;

            if (!attributeExists)
                OnCalculationAdded(this, new EventArgs());
        }

        private void txtName_Validating(object sender, CancelEventArgs e)
        {
            if (string.IsNullOrEmpty(txtName.Text))
            {
                e.Cancel = true;
                errorProvider.SetError(txtName, "Vul een naam in.");
            }
        }

        private void txtSourceEditor_Validating(object sender, CancelEventArgs e)
        {
            if (string.IsNullOrEmpty(txtName.Text))
            {
                e.Cancel = true;
                errorProvider.SetError(txtSourceEditor, "Vul code in.");
            }
        }
    }
}